package org.boyan.esb;

import org.apache.poi.ss.usermodel.*;
import org.boyan.common.util.DateUtils;
import org.boyan.entity.esb.ESBServer;
import org.boyan.utils.ExcelUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ExcelToESB {

    //将Excel文件转化为ESB实体类的方法
    public static List<ESBServer> getESBServer(Object fileObject) {
        Workbook workbook = null;
        if (fileObject instanceof MultipartFile) {
            MultipartFile file = (MultipartFile) fileObject;
            workbook = ExcelUtils.getWorkBook(file);
        } else if (fileObject instanceof File) {
            File file = (File) fileObject;
            workbook = ExcelUtils.getWorkBook(file);
        }
        List<ESBServer> esbServerList=new ArrayList<>();
        try {
            Sheet sheet=workbook.getSheetAt(0);
            int num=sheet.getLastRowNum();
            for(int i=1;i<sheet.getLastRowNum();i++){
                Row row=sheet.getRow(i);
                if(row!=null) {
                    if(row.getCell(3)!=null){
                        ESBServer esbServer = new ESBServer();
                        esbServer.setEsbId(row.getCell(3).getStringCellValue());
                        esbServer.setEsbName(row.getCell(2).getStringCellValue());
                        esbServer.setEsbAction(row.getCell(4).getStringCellValue());
                        //防止数字交易码
                        CellType transCodeCellType = row.getCell(0).getCellType();
                        String transCode;
                        if (transCodeCellType == CellType.NUMERIC) {
                            transCode = String.valueOf(row.getCell(0).getNumericCellValue());
                        } else {
                            transCode = row.getCell(0).getStringCellValue();
                        }
                        esbServer.setTransCode(transCode);
                        esbServerList.add(esbServer);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return esbServerList;
    }
}
